package com.ymatou.iapi.idcardquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardquery.parameter.GetBatchIdCardInfosByNameAndPhoneReq;
import com.ymatou.iapi.idcardquery.parameter.NameAndPhoneInfo;
import com.ymatou.iapi.idcardquery.resp.GetBatchIdCardInfosResp;
import com.ymatou.iapi.idcardquery.resp.IdCardInfo;
import com.ymatou.iapi.idcardquery.resp.IdCardInfoVerify;
import com.ymatou.iapi.idcardquery.resp.RejectReasonEnum;
import com.ymatou.iapi.idcardquery.service.GetBatchIdCardInfosByNameAndPhoneCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
/**
*根据买手ID+批量（收件人+手机号）获取身份证信息(本地 + 贝海)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBatchIdCardInfosByNameAndPhone {
	private static GetBatchIdCardInfosByNameAndPhoneReq bean;
	private static GetBatchIdCardInfosByNameAndPhoneCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据买手ID+批量（收件人+手机号）获取身份证信息(本地 + 贝海)接口");
	}

	@Before
	public void caseUp() {
		bean = new GetBatchIdCardInfosByNameAndPhoneReq();
		call = new GetBatchIdCardInfosByNameAndPhoneCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() throws Exception {

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;
		String oldKey = "1234567890123456";
		String newKey = "123456789";
		GetBatchIdCardInfosResp batchIdCardInfosResp = call
				.getBatchIdCardInfosResp();

		List<IdCardInfo> actuallist = batchIdCardInfosResp.getIdCardInfoList();

		List<YmtIdCardInfo> expectedlist = new ArrayList<YmtIdCardInfo>();

		long buyerId = bean.getBuyerId();

		bean.getIdCardInfos().forEach(
				item -> {

					String name = item.getName();

					YmtIdCardInfo ymtIdCardInfo = ymtIdCard
							.selectByNameAndBuyerId(name, buyerId);

					if (ymtIdCardInfo != null) {

						expectedlist.add(ymtIdCardInfo);
					}
				});

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		for (int i = 0; i < expectedlist.size(); i++) {

			YmtIdCardInfo expectedIdCardInfo = expectedlist.get(i);
			IdCardInfo actualIdCardInfo = actuallist.get(i);

			Logger.comment("校验name:" + expectedIdCardInfo.getName());

			Logger.verifyEquals(expectedIdCardInfo.getCardId(),
					actualIdCardInfo.getCardId(), "校验CardId");

			Logger.verifyEquals(expectedIdCardInfo.getName(),
					actualIdCardInfo.getName(), "校验name");
			Logger.verifyEquals(
					CipherUtil.decryptAES(oldKey, expectedIdCardInfo.getIDCard()),
					actualIdCardInfo.getCardNumber(), "校验CardNumber");

			Logger.verifyEquals(expectedIdCardInfo.getCardType(),
					actualIdCardInfo.getCardType(), "校验CardType");

			Logger.verifyEquals(expectedIdCardInfo.getStatus(),
					actualIdCardInfo.getStatus(), "校验Status");

			Logger.verifyEquals(
					CipherUtil.decryptAES(newKey, expectedIdCardInfo.getMobile()),
					actualIdCardInfo.getMobile(), "校验Mobile");

			String rejectReason = null;

			int rejectReasonCode = expectedIdCardInfo.getRejectReason();

			if (expectedIdCardInfo.getStatus() == -1) {

				RejectReasonEnum rejectReasonEnum = RejectReasonEnum

				.getEnum(rejectReasonCode);

				rejectReason = "审核未通过";

				if (null != rejectReasonEnum) {
					rejectReason = rejectReasonEnum.getMessage();
				}
			}

			String expectedPicRightSide = null;

			String expectedPicReverseSide = null;

			//boolean isHasPic = true;

			if (!expectedIdCardInfo.getRightSide().isEmpty()) {

				String picUrl = IdCardInfoVerify
						.convertPicUrl(expectedIdCardInfo.getRightSide());

				expectedPicRightSide = picUrl;

			} /*else {
				isHasPic = false;

				}*/

			if (!expectedIdCardInfo.getReverseSide().isEmpty()) {

				String picUrl = IdCardInfoVerify
						.convertPicUrl(expectedIdCardInfo.getReverseSide());
				expectedPicReverseSide = picUrl;
			} /*else {
				isHasPic = false;
				}*/

			Logger.verifyEquals(expectedIdCardInfo.getHasPic(),
					actualIdCardInfo.getHasPic(), "校验HasPic");

			Logger.verifyEquals(rejectReason,
					actualIdCardInfo.getRejectReason(), "校验RejectReason");

			Logger.verifyEquals(expectedPicRightSide,
					actualIdCardInfo.getPicRightSide(), "校验picRightSide");
			Logger.verifyEquals(expectedPicReverseSide,
					actualIdCardInfo.getPicReverseSide(), "校验PicReverseSide");

			Logger.verifyEquals(expectedIdCardInfo.getCheckIdCardStatus(),
					actualIdCardInfo.getCheckIdCardStatus(),
					"校验CheckIdCardStatus");

			DataUtil.verifyEquals(expectedIdCardInfo.getUploadTime(),
					actualIdCardInfo.getUploadTime(), "验证UploadTime");

		}

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_001() {
		Logger.start(true, "根据买手ID+批量（收件人+手机号）从本地库获取身份证信息查询成功");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "沈引";
			String phone = "13629748792";
			String name2 = "吴余";
			String phone2 = "13640783856";

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays.asList(
					new NameAndPhoneInfo(name, phone), new NameAndPhoneInfo(
							name2, phone2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	/**
	 * 	SELECT TOP 100 i.* ,p.* FROM dbo.IdentityCard i 
		JOIN dbo.IdMatchPhone p ON i.Id=p.iIdentityCardId 从贝海数据库查询数据
	 */
	public void Tc_GetBatchIdCardInfosByNameAndPhone_002() {
		Logger.start(true, "本地库没有,根据买手ID+批量（收件人+手机号）从贝海获取身份证信息查询成功，"
				+ "并发送mq消息通知身份证管理服务");
		try {

			long buyerId = 20226434;
			String name = "贾小进";
			String phone = "13588526636";

			String name2 = "薛飙";
			String phone2 = "13622006924";

			IdCardInfoVerify.callDeleteIdCardInfo(buyerId, name);
			IdCardInfoVerify.callDeleteIdCardInfo(buyerId, name2);

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays.asList(
					new NameAndPhoneInfo(name, phone), new NameAndPhoneInfo(
							name2, phone2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			Logger.verifyEquals(true, call.getBatchIdCardInfosResp()
					.getIdCardInfoList().size() > 0, "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_003() {
		Logger.start(true, "本地库和贝海都没有数据,接口返回null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "黄英";
			String phone = "18888882222";
			String name2 = "张三2";
			String phone2 = "18888882222";

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays.asList(
					new NameAndPhoneInfo(name, phone), new NameAndPhoneInfo(
							name2, phone2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_004() {
		Logger.start(true, "买家Id为null");
		try {

			//long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "黄英";
			String phone = "18888882222";
			String name2 = "张三2";
			String phone2 = "18888882222";

			//bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays.asList(
					new NameAndPhoneInfo(name, phone), new NameAndPhoneInfo(
							name2, phone2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");
			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("buyerId:buyerId未提供", call.getString("msg"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_005() {
		Logger.start(true, "手机号为null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "黄";
			//String phone = "18888882222";

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays.asList(new NameAndPhoneInfo(name, null)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("idCardInfos[0].phone:手机号未提供",
					call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_006() {
		Logger.start(true, "name为null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			//String name = "黄";
			String phone = "18888882222";

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays
					.asList(new NameAndPhoneInfo(null, phone)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("idCardInfos[0].name:收件人未提供",
					call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_007() {
		Logger.start(true, "idCardInfos列表null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			//String name = "黄";
			//String phone = "18888882222";

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(null);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("idCardInfos:姓名和手机未提供", call.getString("msg"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_008() {
		Logger.start(true, "idCardInfos列表size为0");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			//String name = "黄";
			//String phone = "18888882222";

			List<NameAndPhoneInfo> idCardInfos = new ArrayList<NameAndPhoneInfo>();

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(idCardInfos);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("idCardInfos:姓名和手机未提供", call.getString("msg"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByNameAndPhone_009() {
		Logger.start(true, "idCardInfos列表里含有不存在的用户id,手机号");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "沈引";
			String phone = "18888882222";
			String name2 = "张三2";
			String phone2 = "1888888222";

			bean.setBuyerId(buyerId);

			bean.setIdCardInfos(Arrays.asList(
					new NameAndPhoneInfo(name, phone), new NameAndPhoneInfo(
							name2, phone2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(CipherUtil.decryptAES("123456789", "4FB7861E618334F5DE07FE73B2AFFFA9"));
	}
}